package cn.good.yan;

import cn.good.yan.mapper.UserMapper;
import cn.good.yan.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

/**
 * 测试 2
 *  1 条件构造器   - CURD
 *  2 Lambda写法  - CURD
 *
 * @author shengren.yan
 * @create 2023-09-23
 */
@SpringBootTest
public class test2Select {

    @Autowired
    private UserMapper userMapper;

    /**
     * 1 条件构造器  - CURD
     */
    @Test
    public void test1(){
        // 1. 查询模糊查询、范围查询 between 、不为null
        QueryWrapper<User> q = new QueryWrapper<>();
        q.like("username","a").between("user_type",1,2).isNotNull("password");
        // 排序条件 orderByDesc 降序 orderByAsc 升序
        q.orderByDesc("user_type").orderByAsc("id");
        List<User> users = userMapper.selectList(q);
        users.forEach(System.out::println);

        // 2. 删除条件
        QueryWrapper<User> q1 = new QueryWrapper<>();
        q1.isNull("password");
        int result = userMapper.delete(q1);
        System.out.println(result > 0 ? "删除成功！" : "删除失败！");

        // 3.条件的优先级
        QueryWrapper<User> q3 = new QueryWrapper();
        // select查询 只显示这几个字段，其他字段不显示
        q3.select("username", "real_name", "gender");
        // ge 大于等于 gt 大于 le 小于等于  lt 小于
        q3.gt("gender", 4)
                .and(i -> {
                    i.like("username", "a").or().isNull("real_name");
                }).isNull("real_name");
        List<Map<String, Object>> maps = userMapper.selectMaps(q3);
        // 也可以这样： List<User> maps = userMapper.selectList(q3);

        // 4.修改 - 根据条件进行修改
        UpdateWrapper<User> u = new UpdateWrapper<>();
        u.like("username","a");
        u.set("username","修改的字段").set("real_name","修改的字段2");
        int result2 = userMapper.update(null, u);
        System.out.println(result2 > 0 ? "修改成功！" : "修改失败！");

        // 5.子查询  和 or 或 查询
        QueryWrapper<User> q5 = new QueryWrapper<>();
        q5.inSql("uid", "select uid from 表 where uid <= 100");
        q5.or().isNull("real_name");
        List<User> list = userMapper.selectList(q5);

        // 6.根据条件，判断是否存在， 有代表 true，否则 false
        QueryWrapper<User> qu = new QueryWrapper<>();
        qu.like("name","a");
        boolean exists = userMapper.exists(qu);

        // 7.组装条件 , 参数1 为true，会有该条件
        QueryWrapper<User> qu2 = new QueryWrapper<>();
        qu2.like(null!="a","name","a");
        boolean exists2 = userMapper.exists(qu2);

    }

    /**
     * 2 Lambda写法  - CURD
     */
    @Test
    public void test2(){
        // 1 LambdaQueryWrapper 查询
        String  a = "a";
        Integer b = 10;
        LambdaQueryWrapper<User> q = new LambdaQueryWrapper<>();
        // select查询 只显示这几个字段，其他字段不显示
        q.select(User::getRealName, User::getPassword);
        q.like(StringUtils.isNotBlank(a), User::getRealName, a)
                // ge 大于等于 gt 大于 le 小于等于  lt 小于
                .ge(b != null, User::getGender, b)
                .le(b != null, User::getGender, b)
                .between(User::getGender,1,2)
                .isNull(User::getPassword)
                .isNotNull(User::getPassword)
                // and 和 or 的使用
                .and(i -> {
                    i.like(User::getRealName, "a").or().isNull(User::getPassword);
                })
                // in 查询，范围SQL
                .inSql(User::getId, "select uid from 表 where uid <= 100");
        // 排序条件 orderByDesc 降序 orderByAsc 升序
        q.orderByDesc(User::getUserType).orderByAsc(User::getId);
        List<User> list = userMapper.selectList(q);
        // 1.1  查个数
        Long l = userMapper.selectCount(q);
        // 1.2  查个是否存在
        boolean exists = userMapper.exists(q);

        // 2.LambdaUpdateWrapper
        LambdaUpdateWrapper<User> u = new LambdaUpdateWrapper<>();
        u.like(User::getRealName, "a").isNull(User::getPassword);
        u.set(User::getRealName, "小黑").set(User::getPassword,"xxx");
        int result = userMapper.update(null, u);

    }

}
